﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using static 摸鱼.全局类;
using System.Windows.Media;
using System.Windows.Forms;
using System.Reflection.Metadata;
using System.Windows.Threading;
using System.Windows.Interop;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
using SharpDX.Direct3D11;
using static 摸鱼.键鼠.钩子;
using System.Diagnostics.CodeAnalysis;

namespace 摸鱼
{
    public static class MoyuControl
    {
        public static TextBlock CreatTextBlock(string 文本, double 字体大小, double 空白宽度, double 空白高度)
        {
            System.Windows.Size 文本size = GetSize(文本, 字体大小);
            TextBlock 文本控件 = new TextBlock
            {
                Text = 文本,
                Background = Brushes.LightBlue,
                Padding = new Thickness(0, 空白高度 * 0.5, 0, 0),
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center, // 控件水平居中  
                VerticalAlignment = VerticalAlignment.Center, // 控件垂直居中 
                TextAlignment = System.Windows.TextAlignment.Center, //文字居中
                FontFamily = new FontFamily("Microsoft YaHei UI"),
                FontSize = 字体大小,
                Width = 文本size.Width + 空白宽度,
                Height = 文本size.Height + 空白高度
            };
            return 文本控件;
        }

        public static bool Msgbox(string 内容)
        {
            MoyuWait moyuWait = new(false);
            Application application = new();
            RoutedEventHandler 按钮1event = async (o, e) =>
            {
                (Popup 气泡, string 内容) items = ((Popup, string))((System.Windows.Controls.Button)o).Tag;
                items.气泡.IsOpen = false;
                items.气泡 = null;
                moyuWait.Set(() => true);
                await Task.Delay(100);
                application.Exit();
            };
            RoutedEventHandler 按钮2event = async (o, e) =>
            {
                (Popup 气泡, string 内容) items = ((Popup, string))((System.Windows.Controls.Button)o).Tag;
                items.气泡.IsOpen = false;
                items.气泡 = null;
                moyuWait.Set(() => false);
                await Task.Delay(100);
                application.Exit();
            };
            application.AsyncRun(form => 弹窗按钮(内容, "确定", "取消", 按钮1event, 按钮2event, form));
            return (bool)moyuWait.Get();
        }
        public static void MsgboxThrow([DoesNotReturnIf(true)] bool isBUG, string 文本)
        {
            if (isBUG)
            {
                Msgbox(文本);
                返回顶级调用堆栈();
            }
        }
        public class Application
        {
            public Moyuptr<bool> run = new();
            private ConcurrentQueue<Action> 任务s = new();
            public void Send(Action action)
            {
                ManualResetEventSlim slim = new(false);
                任务s.Enqueue(() =>
                {
                    action();
                    slim.Set();
                });
                slim.Wait();
            }
            public void Post(Action action)
            {
                任务s.Enqueue(action);
            }
            public void Run(Action<Application> action)
            {
                var app = new Application();
                action(app);
                run.Value = true;
                app.任务s = new();
                while (run.Value)
                {
                    System.Windows.Forms.Application.DoEvents();
                    MoyuDelay.Star(10);
                    while (app.任务s.TryDequeue(out var item))
                        item();
                }
            }
            public void AsyncRun(Action<Application> action)
            {
                MoyuThreadPool.Post(() => Run(action));
            }
            public void Exit() => run.Value = false;
        }

        public static void ThrowShow(string 内容)
        {
            Application application = new();
            RoutedEventHandler 按钮1event = async (o, e) =>
            {
                (Popup 气泡, string 内容) items = ((Popup, string))((System.Windows.Controls.Button)o).Tag;
                items.气泡.IsOpen = false;
                items.气泡 = null;
                MainWindow.This.Dispatcher.Invoke(() => System.Windows.Forms.Clipboard.SetDataObject(内容, true, 5, 20));
                await Task.Delay(100);
                application.Exit();
            };
            RoutedEventHandler 按钮2event = async (o, e) =>
            {
                (Popup 气泡, string 内容) items = ((Popup, string))((System.Windows.Controls.Button)o).Tag;
                items.气泡.IsOpen = false;
                items.气泡 = null;
                await Task.Delay(100);
                application.Exit();
            };
            application.AsyncRun(form => 弹窗按钮(内容, "复制", "关闭", 按钮1event, 按钮2event, form));
        }
        public static async void 弹窗按钮(string 内容, string 按钮1文本, string 按钮2文本, RoutedEventHandler 按钮1event, RoutedEventHandler 按钮2event, Application 调用线程)
        {
            double 字体大小 = 12;
            double 文本空行高度 = 99;
            Size 文本size = GetSize(内容, 字体大小);
            TextBlock 文本控件 = CreatTextBlock(内容, 字体大小, 20, 16);

            文本size = GetSize(按钮1文本, 字体大小);
            System.Windows.Controls.Button 按钮1 = new()
            {
                Content = 按钮1文本,
                Margin = new Thickness(0, 1, 0, 0),
                FontFamily = new FontFamily("Microsoft YaHei UI"),
                Background = Brushes.White,
                Width = 文本size.Width * 1.2 + 5,
                Height = 文本size.Height * 1.2
            };
            文本size = GetSize(按钮2文本, 字体大小);
            System.Windows.Controls.Button 按钮2 = new()
            {
                Content = 按钮2文本,
                Margin = new Thickness(0, 1, 0, 8),
                FontFamily = new FontFamily("Microsoft YaHei UI"),
                Background = Brushes.White,
                Width = 文本size.Width * 1.2 + 5,
                Height = 文本size.Height * 1.2
            };

            // 创建一个包含TextBlock和Button的StackPanel  
            StackPanel stackPanel = new StackPanel
            {
                Orientation = System.Windows.Controls.Orientation.Vertical,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Background = Brushes.LightBlue,
                Children = { 文本控件, 按钮1, 按钮2 }
            };

            // 设置Popup的Child为StackPanel  
            Popup 气泡控件 = new Popup
            {
                Child = stackPanel,
                HorizontalOffset = (SystemParameters.WorkArea.Width - stackPanel.ActualWidth) / 2,
                VerticalOffset = (SystemParameters.WorkArea.Height - stackPanel.ActualHeight) / 2
            };
            var tag = (气泡控件, 文本控件.Text);
            按钮1.Tag = tag;
            按钮2.Tag = tag;
            气泡控件.IsOpen = true;

            按钮1.Click += 按钮1event;
            按钮2.Click += 按钮2event;

            Dictionary<Keys, DateTime> enter = new() { { Keys.Enter, default } };
            键鼠.钩子.键盘Click += Enter;
            void Enter(List<int> keys, bool keydown)
            {
                if (keys.全部按下_符合逻辑顺序(enter, keydown))
                {
                    键鼠.钩子.键盘Click -= Enter;
                    调用线程.Send(() => 按钮1event(按钮1, default));
                }
            }
        }
        public static void 气泡(string 文本, MainWindow main, Int32 生命周期 = 500, Action 回调 = default)
        {
            TextBlock 文本控件 = new()
            {
                Text = 文本,
                Background = Brushes.LightBlue,
                Padding = new Thickness(5)
            };
            float 字体大小 = (float)文本控件.FontSize;
            System.Drawing.Size size = System.Windows.Forms.TextRenderer.MeasureText(文本, new System.Drawing.Font("宋体", 字体大小), new System.Drawing.Size(300, 0), System.Windows.Forms.TextFormatFlags.WordBreak);

            Popup 气泡控件 = new()
            {
                PlacementTarget = main,
                Child = 文本控件,
                HorizontalOffset = main.Width / 2 - size.Width / 2,
                VerticalOffset = -main.Height / 2
            };
            气泡控件.IsOpen = true;

            System.Windows.Forms.Timer timer = new()
            {
                Interval = 生命周期 - 10
            };
            timer.Tick += async (o, j) =>
            {
                if (回调 != default)
                    MoyuThreadPool.Post(回调);
                await MoyuThreadPool.Delay(15);
                气泡控件.IsOpen = false;
                气泡控件 = null;
                timer.Dispose();
            };
            timer.Start();
        }
        public class Form_穿透_置顶 : Form
        {
            private ConcurrentQueue<Action> 任务s = new();
            private readonly System.Drawing.Point location;
            protected override CreateParams CreateParams
            {
                get
                {
                    // 获取窗体的创建参数  
                    CreateParams cp = base.CreateParams;
                    // 设置窗体的样式为WS_EX_TRANSPARENT，使窗体透明，这样才能穿透  
                    cp.ExStyle |= 0x20;
                    // 设置窗体的扩展样式为WS_EX_LAYERED，使窗体可以透明度控制，然后可以调整透明度  
                    cp.ExStyle |= 0x80000;
                    return cp;
                }
            }
            public Form_穿透_置顶(System.Drawing.Point point)
            {
                FormBorderStyle = FormBorderStyle.None;
                BackColor = System.Drawing.Color.FromArgb(255, 255, 186);
                TransparencyKey = System.Drawing.Color.FromArgb(255, 255, 186);
                ShowInTaskbar = false;
                location = point;
                Shown += load_show;
                Activated += load_show;
                Load += load_show;
            }
            public Form_穿透_置顶(System.Drawing.Rectangle rect)
            {
                FormBorderStyle = FormBorderStyle.None;
                BackColor = System.Drawing.Color.FromArgb(255, 255, 186);
                TransparencyKey = System.Drawing.Color.FromArgb(255, 255, 186);
                ShowInTaskbar = false;
                Bounds = rect;
                location = new(rect.X, rect.Y);
                Shown += load_show;
                Activated += load_show;
                Load += load_show;
            }
            public void Post(Action item)
            {
                任务s.Enqueue(item);
            }
            public static Form_穿透_置顶 CreateAsyncShow(核心._数据._方法data 方法data, System.Drawing.Rectangle rect, Action action = default, int 刷新间隔 = 10)
            {
                Form_穿透_置顶 form = null;
                MoyuThreadPool.Post(() =>
                {
                    form = new(rect);
                    form.show(方法data, action, 刷新间隔);
                });
                while (form == null)
                    MoyuDelay.Star(10);
                return form;
            }
            public void AsyncShow(核心._数据._方法data 方法data, Action action = default, int 刷新间隔 = 10)
            {
                MoyuThreadPool.Post(() => show(方法data, action, 刷新间隔));
                MoyuDelay.Star(80);
            }
            private void show(核心._数据._方法data 方法data, Action action, int 刷新间隔)
            {
                this.Show();
                var hand = this.Handle;
                if (action != null)
                    action();
                while (!方法data.isDispose)
                {
                    System.Windows.Forms.Application.DoEvents();
                    MoyuDelay.Star(刷新间隔);
                    while (任务s.TryDequeue(out var item))
                        item();
                }
            }

            public void load_show(object o, EventArgs e)
            {
                Location = location;
                TopMost = true;
            }
            //protected override void OnPaint(PaintEventArgs e)
            //{
            //    base.OnPaint(e);
            //    // 绘制自定义边框（例如，只绘制底部边框）
            //    System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 5);
            //    e.Graphics.DrawLine(pen, 0, this.ClientSize.Height - 1, this.ClientSize.Width, this.ClientSize.Height - 1);
            //    pen.Dispose();
            //}
        }






















    }
}
